﻿using System;
using System.Diagnostics;
using System.Text;

namespace AmorCanino.Common.Logger
{
    public class EventLogger
        : ILogger
    {
        public void WriteLog(LogEntry logEntry)
        {
            WriteALog(logEntry);
        }

        public void WriteLog(LogEntry logEntry, LogLevels logLevel)
        {
            this.LogLevel = logLevel;
            WriteALog(logEntry);
        }

        public void WriteLog(LogEntry logEntry, LogLevels logLevel, LogDetails logDetail)
        {
            this.LogLevel = logLevel;
            this.LogDetail = logDetail;
            WriteALog(logEntry);
        }

        public LogLevels LogLevel
        {
            get;
            set;
        }

        public LogDetails LogDetail
        {
            get;
            set;
        }

        /// <summary>
        /// Makes the log
        /// </summary>
        /// <param name="logEntry"></param>
        private void WriteALog(LogEntry logEntry)
        {
            if (logEntry == null)
                throw new ArgumentNullException("logEntry", "logEntry cannot be null.");

            StringBuilder sb = new StringBuilder();

            switch (LogDetail)
            {
                case LogDetails.Information:
                    sb.Append(logEntry.Message);
                    break;
                case LogDetails.Verbose:
                    sb.AppendLine(logEntry.Message);
                    sb.AppendLine(logEntry.Source.ToString());
                    sb.AppendLine(LogLevel == LogLevels.Error
                                    ? Environment.StackTrace
                                    : "");
                    break;
                default:
                    sb.Append(logEntry.Message);
                    break;
            }

            EventLog.WriteEntry(logEntry.Application, sb.ToString(),
                    LogLevel == LogLevels.Error
                    ? EventLogEntryType.Error
                    : LogLevel == LogLevels.Information
                        ? EventLogEntryType.Information
                        : LogLevel == LogLevels.Warning
                            ? EventLogEntryType.Warning 
                            : EventLogEntryType.Information);
        }
    }
}
